package mspbots.qbd.online.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import mspbots.qbd.common.RabbitKeys;
import org.springframework.amqp.core.*;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * com.mspbots.sync.config.RabbitConfigurer
 *
 * @author Alex bob(https://github.com/vnobo)
 * @date Created by 2019/7/29
 */
@Configuration
public class RabbitConfiguration {

    private final AmqpAdmin amqpAdmin;

    Exchange exchange = ExchangeBuilder.directExchange(RabbitKeys.BASE_EXCHANGE_NAME).build();
    Map<String, Object> args = new HashMap<>(4) {{
        put("x-dead-letter-exchange", RabbitKeys.BASE_EXCHANGE_NAME);
        put("x-dead-letter-routing-key", RabbitKeys.BAD_MESSAGE_QUEUE_KEY);
    }};

    public RabbitConfiguration(AmqpAdmin amqpAdmin) {
        this.amqpAdmin = amqpAdmin;
        this.amqpAdmin.declareExchange(exchange);
    }

    @Bean
    public void durableTaskQueue() {
        Queue queue = QueueBuilder.durable("quick-books.bad.message")
                .withArgument("x-message-ttl", 1000 * 60 * 60 * 24).build();
        this.amqpAdmin.declareQueue(queue);
        this.amqpAdmin.declareBinding(BindingBuilder.bind(queue).to(exchange).with(RabbitKeys.BAD_MESSAGE_QUEUE_KEY).noargs());

        queue = QueueBuilder.durable("quick-books-online.distributed.task.sync")
                .withArguments(args).build();
        this.amqpAdmin.deleteQueue(queue.getName());
        this.amqpAdmin.declareQueue(queue);
        this.amqpAdmin.declareBinding(BindingBuilder.bind(queue).to(exchange)
                .with(RabbitKeys.DISTRIBUTED_TASK_QUEUE_KEY).noargs());

        queue = QueueBuilder.durable("quick-books.logs.task.sync").withArguments(args).build();
        this.amqpAdmin.declareQueue(queue);
        this.amqpAdmin.declareBinding(BindingBuilder.bind(queue).to(exchange)
                .with(RabbitKeys.LOGS_TASK_QUEUE_KEY).noargs());
    }

    @Bean
    public void declareExchange() {

        Queue queue = QueueBuilder.durable("quick-books-online.account.sync").withArguments(args).build();
        this.amqpAdmin.declareQueue(queue);
        this.amqpAdmin.declareBinding(BindingBuilder.bind(queue)
                .to(exchange).with(RabbitKeys.ACCOUNT_ONLINE_SYNC_QUEUE_KEY).noargs());

        queue = QueueBuilder.durable("quick-books-online.ProfitAndLoss.standard.sync").withArguments(args).build();
        this.amqpAdmin.declareQueue(queue);
        this.amqpAdmin.declareBinding(BindingBuilder.bind(queue)
                .to(exchange).with(RabbitKeys.PROFITANDLOSS_STANDARD_ONLINE_SYNC_QUEUE_KEY).noargs());

        queue = QueueBuilder.durable("quick-books-online.ProfitAndLoss.detail.sync").withArguments(args).build();
        this.amqpAdmin.declareQueue(queue);
        this.amqpAdmin.declareBinding(BindingBuilder.bind(queue)
                .to(exchange).with(RabbitKeys.PROFITANDLOSS_DETAIL_ONLINE_SYNC_QUEUE_KEY).noargs());

        queue = QueueBuilder.durable("quick-books-online.invoice.sync").withArguments(args).build();
        this.amqpAdmin.declareQueue(queue);
        this.amqpAdmin.declareBinding(BindingBuilder.bind(queue)
                .to(exchange).with(RabbitKeys.INVOICE_ONLINE_SYNC_QUEUE_KEY).noargs());

        queue = QueueBuilder.durable("quick-books-online.journal.entry.sync").withArguments(args).build();
        this.amqpAdmin.declareQueue(queue);
        this.amqpAdmin.declareBinding(BindingBuilder.bind(queue)
                .to(exchange).with(RabbitKeys.JOURNAL_ENTRY_ONLINE_SYNC_QUEUE_KEY).noargs());

        queue = QueueBuilder.durable("quick-books-online.preferences.sync").withArguments(args).build();
        this.amqpAdmin.declareQueue(queue);
        this.amqpAdmin.declareBinding(BindingBuilder.bind(queue)
                .to(exchange).with(RabbitKeys.PREFERENCES_ONLINE_SYNC_QUEUE_KEY).noargs());

        queue = QueueBuilder.durable("quick-books-online.company.info.sync").withArguments(args).build();
        this.amqpAdmin.declareQueue(queue);
        this.amqpAdmin.declareBinding(BindingBuilder.bind(queue)
                .to(exchange).with(RabbitKeys.COMPANY_INFO_ONLINE_SYNC_QUEUE_KEY).noargs());

        queue = QueueBuilder.durable("quick-books-online.customer.sync").withArguments(args).build();
        this.amqpAdmin.declareQueue(queue);
        this.amqpAdmin.declareBinding(BindingBuilder.bind(queue)
                .to(exchange).with(RabbitKeys.CUSTOMER_ONLINE_SYNC_QUEUE_KEY).noargs());

        queue = QueueBuilder.durable("quick-books-online.balance.sheet.sync").withArguments(args).build();
        this.amqpAdmin.declareQueue(queue);
        this.amqpAdmin.declareBinding(BindingBuilder.bind(queue)
                .to(exchange).with(RabbitKeys.BALANCE_SHEET_ONLINE_SYNC_QUEUE_KEY).noargs());


        queue = QueueBuilder.durable("quick-books-online.general.ledger.sync").withArguments(args).build();
        this.amqpAdmin.declareQueue(queue);
        this.amqpAdmin.declareBinding(BindingBuilder.bind(queue)
                .to(exchange).with(RabbitKeys.GENERAL_LEDGER_ONLINE_SYNC_QUEUE_KEY).noargs());

        queue = QueueBuilder.durable("quick-books-online.bill.sync").withArguments(args).build();
        this.amqpAdmin.declareQueue(queue);
        this.amqpAdmin.declareBinding(BindingBuilder.bind(queue)
                .to(exchange).with(RabbitKeys.BILL_ONLINE_SYNC_QUEUE_KEY).noargs());

        queue = QueueBuilder.durable("quick-books-online.employee.sync").withArguments(args).build();
        this.amqpAdmin.declareQueue(queue);
        this.amqpAdmin.declareBinding(BindingBuilder.bind(queue)
                .to(exchange).with(RabbitKeys.EMPLOYEE_ONLINE_SYNC_QUEUE_KEY).noargs());

        queue = QueueBuilder.durable("quick-books-online.item.sync").withArguments(args).build();
        this.amqpAdmin.declareQueue(queue);
        this.amqpAdmin.declareBinding(BindingBuilder.bind(queue)
                .to(exchange).with(RabbitKeys.ITEM_ONLINE_SYNC_QUEUE_KEY).noargs());

        queue = QueueBuilder.durable("quick-books-online.tax.agency.sync").withArguments(args).build();
        this.amqpAdmin.declareQueue(queue);
        this.amqpAdmin.declareBinding(BindingBuilder.bind(queue)
                .to(exchange).with(RabbitKeys.TAX_AGENCY_ONLINE_SYNC_QUEUE_KEY).noargs());

        queue = QueueBuilder.durable("quick-books-online.estimate.sync").withArguments(args).build();
        this.amqpAdmin.declareQueue(queue);
        this.amqpAdmin.declareBinding(BindingBuilder.bind(queue)
                .to(exchange).with(RabbitKeys.ESTIMATE_ONLINE_SYNC_QUEUE_KEY).noargs());

        queue = QueueBuilder.durable("quick-books-online.payment.sync").withArguments(args).build();
        this.amqpAdmin.declareQueue(queue);
        this.amqpAdmin.declareBinding(BindingBuilder.bind(queue)

                .to(exchange).with(RabbitKeys.PAYMENT_ONLINE_SYNC_QUEUE_KEY).noargs());

        queue = QueueBuilder.durable("quick-books-online.vendor.sync").withArguments(args).build();
        this.amqpAdmin.declareQueue(queue);
        this.amqpAdmin.declareBinding(BindingBuilder.bind(queue)
                .to(exchange).with(RabbitKeys.VENDOR_ONLINE_SYNC_QUEUE_KEY).noargs());

        queue = QueueBuilder.durable("quick-books-online.general.ledger.sync").withArguments(args).build();
        this.amqpAdmin.declareQueue(queue);
        this.amqpAdmin.declareBinding(BindingBuilder.bind(queue)
                .to(exchange).with(RabbitKeys.GENERAL_LEDGER_ONLINE_SYNC_QUEUE_KEY).noargs());

        queue = QueueBuilder.durable("quick-books-online.deposit.sync").withArguments(args).build();
        this.amqpAdmin.declareQueue(queue);
        this.amqpAdmin.declareBinding(BindingBuilder.bind(queue)
                .to(exchange).with(RabbitKeys.DEPOSIT_ONLINE_SYNC_QUEUE_KEY).noargs());


        queue = QueueBuilder.durable("quick-books-online.credit.memo.sync").withArguments(args).build();
        this.amqpAdmin.declareQueue(queue);
        this.amqpAdmin.declareBinding(BindingBuilder.bind(queue)
                .to(exchange).with(RabbitKeys.CREDIT_MEMO_ONLINE_SYNC_QUEUE_KEY).noargs());



    }

    @Bean
    public MessageConverter rabbitMessageConverter(ObjectMapper objectMapper) {
        return new Jackson2JsonMessageConverter(objectMapper);
    }


}
